﻿// qstata 01b_call_append_ECs.do --memsize=64
**Objective: Call to fn to pull all ECs across all years with ID and economic vars

/* Note: raw variable names have been changed in the code files to abide by Census disclosure guidelines */

*Set directories 
global import_dir = "/projects/data_p/import/"
global build_raw_dir = "/projects/data_p/build/raw/"
global build_mst_dir = "/projects/data_p/build/mst/"
global import_code_dir = "/projects/hsieh_ra/import/"
global build_code_dir = "/projects/hsieh_ra/build/"
global data_commons = "/projects/data_commons/"

*CMF
use "${build_raw_dir}03_cmf/cmf_appended.dta", clear
gen final_sales = final_sales2
replace final_sales = final_sales3 if mi(final_sales2)
save "${build_raw_dir}03_cmf/02_cmf_sales.dta", replace
do "${build_code_dir}01a_append_ECs_fn.do" "02_cmf_sales.dta" "03_cmf" "final_sales" "final_emp1 < 0 | final_sales2 < 0 | final_sales3 < 0 | final_salaries5 < 0 | final_salaries1 < 0" "CMF" "final_emp1" "final_salaries1"
 
*CFI
do "${build_code_dir}01a_append_ECs_fn.do" "cfi_appended.dta" "04_cfi" "final_sales1" "final_emp2 < 0 | final_sales1 < 0 | final_salaries2 < 0" "CFI" "final_emp2" "final_salaries2"

*CCN 
use "${build_raw_dir}05_ccn/ccn_appended.dta", clear 
gen final_sales = final_sales2
replace final_sales = TR if mi(final_sales)
replace final_sales = final_sales3 if mi(final_sales)
gen final_emp = final_emp3 
replace final_emp = final_emp4 if mi(final_emp)
gen final_salaries = final_salaries3
replace final_salaries = final_salaries4 if mi(final_salaries)
replace final_salaries = final_salaries1 if mi(final_salaries)
replace final_salaries = final_salaries5 if mi(final_salaries)
save "${build_raw_dir}05_ccn/02_ccn_sales.dta", replace 
do "${build_code_dir}01a_append_ECs_fn.do" "02_ccn_sales.dta" "05_ccn" "final_sales" "final_sales < 0 | final_emp3 < 0 | final_salaries3 < 0 | final_emp4 < 0 | EMPSUM < 0 | final_salaries5 < 0 | final_salaries4 < 0 | EMPSUMT < 0 | final_salaries6  < 0 | final_salaries1 < 0" "CCN" "final_emp" "final_salaries"
 
*CRT
do "${build_code_dir}01a_append_ECs_fn.do" "crt_appended.dta" "06_crt" "final_sales1" "final_sales1 < 0 | final_emp2 < 0 | final_salaries2 < 0" "CRT" "final_emp2" "final_salaries2"
 
*CSR 
do "${build_code_dir}01a_append_ECs_fn.do" "csr_appended.dta" "07_csr" "final_sales1" "final_sales1 < 0 | final_emp2 < 0 | final_salaries2 < 0" "CSR" "final_emp2" "final_salaries2"
 
*CUT:
do "${build_code_dir}01a_append_ECs_fn.do" "cut_appended.dta" "08_cut" "final_sales1" "final_sales1 < 0 | final_emp2 < 0 | final_salaries2 < 0" "CUT" "final_emp2" "final_salaries2"

*CWH:
do "${build_code_dir}01a_append_ECs_fn.do" "cwh_appended.dta" "09_cwh" "final_sales1" "final_sales1 < 0 | final_emp2 < 0 | final_salaries2 < 0" "CWH" "final_emp2" "final_salaries2"
 
**APPENDING:
cd "${build_raw_dir}10_sales_data/"
local filelist: dir . files "*sales.dta"
append using `filelist'
gen drop = .
replace drop = 1 if lbdid_v16 == "" & cen_sur_num == ""
bys YEAR lbdid_v16 cen_sur_num: replace drop = 1 if _n > 1 
bys YEAR lbdid_v16 cen_sur_num: egen drop_sum = sum(drop)
replace drop = 1 if drop_sum >= 1
save "${build_mst_dir}01a_full_EC_appended.dta", replace 
*Keeping 1st obs of duplicates with same final_sales reported 
bys YEAR lbdid_v16 cen_sur_num: egen sd_sales = sd(final_sales)
bys YEAR lbdid_v16 cen_sur_num: replace drop = . if sd_sales == 0 & _n == 1
replace drop = 1 if lbdid_v16 == "" & cen_sur_num == ""
keep if drop == .
drop drop* sd_sales*
save "${build_mst_dir}01b_unique_EC_appended.dta", replace
saveold "${build_mst_dir}01b_unique_EC_appended_v12.dta", replace version(12)
 

use "${build_mst_dir}01a_full_EC_appended.dta", clear

keep if drop == 1

drop drop* 

save "${build_mst_dir}01c_duplicates_EC_appended.dta", replace
